#ifndef _LIGHTNING_H_
#define _LIGHTNING_H_


#include "sfx.h"
#include "ui/ui_sprite.h"
#include "math/counter.h"
#include <vector>

namespace vfx
{

class		Lightning : public SFXSet
{
public:
	typedef		void (*RayCB)(const math::Vec2& From, const math::Vec2& To, int	Idx, void* pData);
	struct		RaySegment
	{
		math::Vec2		From, To, Normal;
		float			Alpha, Jitter, Width;
	};
	struct		ChunkData
	{
		math::Vec2		From, To;
		float			Jitter, Alpha;
		int				Iterations;
	};
	struct		Bolt
	{
		std::vector<ChunkData>				Chunks;
		std::vector<RaySegment>			Segments;	// sections to render
		math::Counter					Delay;		// how long to wait before starting rendering/ticking
		math::Counter					Duration;	// how long is it visible? remember to fade it out
		int								RegenC, RegenI;		// Do we just recalculate?
		bool							IsMain;
		math::Vec2						From, To;	// the original position
		RayCB							pCB;
		void*							pData;
	};
	Lightning(EffectMan*		pParent);
	~Lightning();
	void	SetTexture(const std::string& Name) { m_Sprite.Create(Name); };
	void	TriggerBolt(const math::Vec2& From, const math::Vec2& To, RayCB pCB = NULL, void* pData = NULL);
	void	Tick(float dt);
	void	Render(const math::Vec2&	Offset, float Scale);
	void	RenderDebug(const math::Vec2&	Offset, float Scale);
	unsigned int BoltC() { return m_Bolts.size(); };
private:
	Bolt*	StartBolt(const math::Vec2& From, const math::Vec2& To);
	void	Generate(Bolt* pBolt, const math::Vec2& From, const math::Vec2& To, float Jitter, int	Recursions, float Alpha);
	int		GetIterations(float Dist);
	std::vector<Bolt*>			m_Bolts;
	ui::UISprite				m_Sprite;
	
public:
	float						m_Jitter;				// amount of midpoint jitter to start with
	float						m_JitterReduction;		// how much less is the jitter for each iteration
	int							m_Recursions;			// max# of iterations. Should this be dependent on size instead?
	int							m_BranchProb;			// probability (%) of a fork branching
	float						m_BranchDeviation;		// what's the angular offset of the branch (represented as a fraction of the length of the segment)
	float						m_BranchAlpha;			// what's the alpha multiplier for branching
	float						m_BranchLength;			// what's the length as fraction of normal segment?
	float						m_Duration;				// how long is an individual ray up
	int							m_RepeatC;				// how many iterations make up a single bolt?
	float						m_RayWidth;		
};

}  // namespace vfx

#endif // _LIGHTNING_H_


